import java.io.*;
import java.util.Scanner;

public class test {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文件名:");
        String fileName = scanner.nextLine();
        File file = new File("/D:"+fileName);
        if(!file.exists()) {
            System.out.println("请输入文件内容:");
            String content = scanner.nextLine();
            file.createNewFile();
            //写文件
            OutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(file);
                byte[] buffer = content.getBytes();
                outputStream.write(buffer);
                outputStream.flush();
            } finally {
                outputStream.close();
            }
            System.out.println("D:\\" + fileName + "的内容是:");
            System.out.println(content);
        }else {
            //读文件
            InputStream inputStream = null;
            try {
                inputStream = new FileInputStream(file);
                while (true) {
                    byte[] buffer = new byte[1024];
                    int n = inputStream.read(buffer);
                    if(n == -1) {
                        break;
                    }
                    String s = new String(buffer,0,n);
                    System.out.println("D:\\" + fileName + "的内容是:");
                    System.out.print(s);
                }
            }finally {
                inputStream.close();
            }
        }
    }
}
